{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-01-07T06:50:47.573796Z",
     "start_time": "2025-01-07T06:50:46.987415Z"
    }
   },
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T06:51:14.932322Z",
     "start_time": "2025-01-07T06:51:14.918945Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#可直接使用 NumPy 的函数\n",
    "# Numpy ufunc 函数\n",
    "df = pd.DataFrame(np.random.randn(5,4) - 1)\n",
    "print(df)\n",
    "print(np.abs(df))"
   ],
   "id": "a99b99a59af60f36",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         1         2         3\n",
      "0 -1.352153 -0.054314 -2.903626 -2.940963\n",
      "1 -0.169521  0.373905 -0.272458 -1.828113\n",
      "2 -3.421275 -1.358957 -1.730346 -0.925008\n",
      "3 -1.350046 -0.021001 -2.360520 -1.893590\n",
      "4 -1.143806 -1.107979 -0.897261 -0.427083\n",
      "          0         1         2         3\n",
      "0  1.352153  0.054314  2.903626  2.940963\n",
      "1  0.169521  0.373905  0.272458  1.828113\n",
      "2  3.421275  1.358957  1.730346  0.925008\n",
      "3  1.350046  0.021001  2.360520  1.893590\n",
      "4  1.143806  1.107979  0.897261  0.427083\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T06:52:12.103315Z",
     "start_time": "2025-01-07T06:52:12.095809Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#通过 apply 将函数应用到列或行上\n",
    "print(df.apply(lambda x : x.max()))"
   ],
   "id": "472f9b15229f0a0e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0   -0.169521\n",
      "1    0.373905\n",
      "2   -0.272458\n",
      "3   -0.427083\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T06:52:48.236852Z",
     "start_time": "2025-01-07T06:52:48.223713Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 指定轴方向， axis=1， 方向是行\n",
    "print(df.apply(lambda x : x.max(), axis=1))"
   ],
   "id": "be1e93317d0cf12d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0   -0.054314\n",
      "1    0.373905\n",
      "2   -0.925008\n",
      "3   -0.021001\n",
      "4   -0.427083\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T06:53:41.504394Z",
     "start_time": "2025-01-07T06:53:41.497842Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#通过 map 将函数应用到每个数据上\n",
    "f2 = lambda x : '%.2f' % x\n",
    "print(df.map(f2))"
   ],
   "id": "1212e4f3da10eaca",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       0      1      2      3\n",
      "0  -1.35  -0.05  -2.90  -2.94\n",
      "1  -0.17   0.37  -0.27  -1.83\n",
      "2  -3.42  -1.36  -1.73  -0.93\n",
      "3  -1.35  -0.02  -2.36  -1.89\n",
      "4  -1.14  -1.11  -0.90  -0.43\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T06:57:14.509638Z",
     "start_time": "2025-01-07T06:57:14.494005Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#索引排序\n",
    "# Series\n",
    "s4 = pd.Series(range(10, 15), index = np.random.randint(5, size=5))\n",
    "print(s4)\n",
    "# 索引排序\n",
    "s4.sort_index()"
   ],
   "id": "80401738747b6096",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    10\n",
      "4    11\n",
      "3    12\n",
      "3    13\n",
      "0    14\n",
      "dtype: int64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0    10\n",
       "0    14\n",
       "3    13\n",
       "3    12\n",
       "4    11\n",
       "dtype: int64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T06:57:59.292365Z",
     "start_time": "2025-01-07T06:57:59.281872Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# DataFrame\n",
    "df4 = pd.DataFrame(np.random.randn(3, 5),\n",
    "index=np.random.randint(3, size=3),\n",
    "columns=np.random.randint(5, size=5))\n",
    "print(df4)\n",
    "df4_isort = df4.sort_index(axis=1, ascending=False)\n",
    "print(df4_isort)"
   ],
   "id": "5c067715030b40ee",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          2         3         3         1         2\n",
      "2 -0.642747 -0.559678  2.291804 -2.434347  0.344418\n",
      "0  0.110925  0.184078 -1.465184 -0.991987  1.366430\n",
      "2  0.874758  0.255917 -0.858843  1.250604 -0.527998\n",
      "          3         3         2         2         1\n",
      "2 -0.559678  2.291804 -0.642747  0.344418 -2.434347\n",
      "0  0.184078 -1.465184  0.110925  1.366430 -0.991987\n",
      "2  0.255917 -0.858843  0.874758 -0.527998  1.250604\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T07:00:51.954782Z",
     "start_time": "2025-01-07T07:00:51.948225Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#按值排序\n",
    "df4_vsort = df4.sort_values(by=1, ascending=False)\n",
    "print(df4_vsort)"
   ],
   "id": "3f8bd7d8ed4d5060",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          2         3         3         1         2\n",
      "2  0.874758  0.255917 -0.858843  1.250604 -0.527998\n",
      "0  0.110925  0.184078 -1.465184 -0.991987  1.366430\n",
      "2 -0.642747 -0.559678  2.291804 -2.434347  0.344418\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T07:01:37.379504Z",
     "start_time": "2025-01-07T07:01:37.371224Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#处理缺失数据\n",
    "df_data = pd.DataFrame([np.random.randn(3), [1., 2., np.nan],[np.nan, 4., np.nan], [1., 2., 3.]])\n",
    "print(df_data.head())"
   ],
   "id": "f1bc3181a558fc42",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         1         2\n",
      "0  0.562927  1.481764 -1.220148\n",
      "1  1.000000  2.000000       NaN\n",
      "2       NaN  4.000000       NaN\n",
      "3  1.000000  2.000000  3.000000\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T07:29:05.174588Z",
     "start_time": "2025-01-07T07:29:05.162771Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 判断是否存在缺失值：isnull\n",
    "print(df_data.isnull())"
   ],
   "id": "a1f66b3f34e7b46a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       0      1      2\n",
      "0  False  False  False\n",
      "1  False  False   True\n",
      "2   True  False   True\n",
      "3  False  False  False\n"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T07:29:37.487856Z",
     "start_time": "2025-01-07T07:29:37.479628Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 丢弃缺失数据： dropna\n",
    "print(df_data.dropna())\n",
    "print(df_data.dropna(axis=1))"
   ],
   "id": "ac5001d2ee56ea8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         1         2\n",
      "0  0.562927  1.481764 -1.220148\n",
      "3  1.000000  2.000000  3.000000\n",
      "          1\n",
      "0  1.481764\n",
      "1  2.000000\n",
      "2  4.000000\n",
      "3  2.000000\n"
     ]
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T07:31:42.155693Z",
     "start_time": "2025-01-07T07:31:42.146114Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 填充缺失数据： fillna\n",
    "print(df_data.fillna(-100.))"
   ],
   "id": "fb58cb10131b7131",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "            0         1           2\n",
      "0    0.562927  1.481764   -1.220148\n",
      "1    1.000000  2.000000 -100.000000\n",
      "2 -100.000000  4.000000 -100.000000\n",
      "3    1.000000  2.000000    3.000000\n"
     ]
    }
   ],
   "execution_count": 16
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
